package com.cleaningcloud.cld.service.impl;

import com.cleaningcloud.cld.domain.CldInsurance;
import com.cleaningcloud.cld.domain.CldInsuranceStaff;
import com.cleaningcloud.cld.domain.CldInsuranceStatics;
import com.cleaningcloud.cld.domain.dto.CldInsuranceDto;
import com.cleaningcloud.cld.domain.dto.CldInsuranceStaffDto;
import com.cleaningcloud.cld.domain.excelExport.InsuranceExcelExport;
import com.cleaningcloud.cld.domain.excelExport.InsuranceResignExcelExport;
import com.cleaningcloud.cld.domain.vo.CldInsuranceStaffVo;
import com.cleaningcloud.cld.domain.vo.CldInsuranceVo;
import com.cleaningcloud.cld.mapper.CldInsuranceMapper;
import com.cleaningcloud.cld.mapper.CldInsuranceStaffMapper;
import com.cleaningcloud.cld.mapper.CldStaffMapper;
import com.cleaningcloud.cld.service.ICldInsuranceService;
import com.cleaningcloud.common.core.utils.DateUtils;
import com.cleaningcloud.common.security.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 保险管理Service业务层处理
 *
 * @author cleaningcloud
 * @date 2024-10-22
 */
@Service
public class CldInsuranceServiceImpl implements ICldInsuranceService {
    @Autowired
    private CldInsuranceMapper cldInsuranceMapper;

    @Autowired
    private CldInsuranceStaffMapper insuranceStaffMapper;

    @Autowired
    private CldStaffMapper cldStaffMapper;

    /**
     * 查询保险管理
     *
     * @param insuranceId 保险管理主键
     * @return 保险管理
     */
    @Override
    public CldInsurance selectCldInsuranceByInsuranceId(Long insuranceId) {
        return cldInsuranceMapper.selectCldInsuranceByInsuranceId(insuranceId);
    }

    /**
     * 查询保险管理列表
     *
     * @param cldInsurance 保险管理
     * @return 保险管理
     */
    @Override
    public List<CldInsuranceVo> selectCldInsuranceList(CldInsurance cldInsurance) {
        return cldInsuranceMapper.selectCldInsuranceList(cldInsurance);
    }

    /**
     * 新增保险管理
     *
     * @param cldInsurance 保险管理
     * @return 结果
     */
    @Override
    public int insertCldInsurance(CldInsurance cldInsurance) {
        cldInsurance.setCreateTime(DateUtils.getNowDate());
        cldInsurance.setCreateId(SecurityUtils.getUserId());
        cldInsurance.setCreateName(SecurityUtils.getUsername());
        cldInsurance.setFlag(0L);
        cldInsurance.setInsuranceStatus(1);
//        cldInsurance.setInsuranceOdd(UniqueIdUtils.generateUniqueId());
        return cldInsuranceMapper.insertCldInsurance(cldInsurance);
    }

    /**
     * 修改保险管理
     *
     * @param cldInsurance 保险管理
     * @return 结果
     */
    @Override
    public int updateCldInsurance(CldInsurance cldInsurance) {
        return cldInsuranceMapper.updateCldInsurance(cldInsurance);
    }

    /**
     * 批量删除保险管理
     *
     * @param insuranceIds 需要删除的保险管理主键
     * @return 结果
     */
    @Override
    public int deleteCldInsuranceByInsuranceIds(Long[] insuranceIds) {
        return cldInsuranceMapper.deleteCldInsuranceByInsuranceIds(insuranceIds);
    }

    @Override
    @Transactional
    public int relatedPersonnel(CldInsuranceStaffDto cldInsuranceStaffDto) {
        List<CldInsuranceStaff> list = new ArrayList<>();
        if (cldInsuranceStaffDto.getStaffIds().size() > 0) {
            for (Long staffId : cldInsuranceStaffDto.getStaffIds()) {
                CldInsuranceStaff cldInsuranceStaff = new CldInsuranceStaff();
                cldInsuranceStaff.setInsuranceId(cldInsuranceStaffDto.getInsuranceId());
                cldInsuranceStaff.setOperaStatus(cldInsuranceStaffDto.getOperaStatus());
                cldInsuranceStaff.setStaffId(staffId);
                cldInsuranceStaff.setNewInsuranceId(cldInsuranceStaff.getInsuranceId());
                list.add(cldInsuranceStaff);
                // 新增
                if (cldInsuranceStaffDto.getOperaStatus() == 1) {
                    cldStaffMapper.updateInsuranceIdByStaffId(cldInsuranceStaffDto.getInsuranceId(), staffId);
                }
                // 解绑
                if (cldInsuranceStaffDto.getOperaStatus() == 3) {
                    cldStaffMapper.updateInsuranceIdByStaffId(null, staffId);
                }
            }
            insuranceStaffMapper.batchInsert(list);
        }
        return 1;
    }

    /**
     * 删除保险管理信息
     *
     * @param insuranceId 保险管理主键
     * @return 结果
     */
    @Override
    public int deleteCldInsuranceByInsuranceId(Long insuranceId) {
        return cldInsuranceMapper.deleteCldInsuranceByInsuranceId(insuranceId);
    }

    @Override
    public List<CldInsuranceStatics> insuranceStatistics(CldInsuranceStatics cldInsuranceStatics) {
        return cldInsuranceMapper.insuranceStatistics(cldInsuranceStatics);
    }

    @Override
    public Map<String, Object> selectCldInsuranceAnalyze() {
        return cldInsuranceMapper.selectCldInsuranceAnalyze();
    }

    @Override
    public List<CldInsuranceVo> selectInsuranceStaff(CldInsuranceDto cldInsuranceDto) {
        return cldInsuranceMapper.selectInsuranceStaff(cldInsuranceDto);
    }

    @Override
    public List<Map<String, Object>> selectCldInsuranceStaffStatisticsList(CldInsuranceStaffDto cldInsuranceStaffDto) {
        return insuranceStaffMapper.selectCldInsuranceStaffStatisticsList(cldInsuranceStaffDto);
    }

    @Override
    public List<CldInsuranceStaffVo> selectCldInsuranceStaffList(CldInsuranceStaffDto cldInsuranceStaffDto) {
        return insuranceStaffMapper.selectCldInsuranceStaffList(cldInsuranceStaffDto);
    }

    @Override
    public List<InsuranceExcelExport> excelExport() {
        return insuranceStaffMapper.notInInsuranceStaffList();
    }

    @Override
    public List<InsuranceResignExcelExport> excelResignExport() {
        return insuranceStaffMapper.resignInsuranceStaffList();
    }
}
